package com.in.mobilerechargedth;

import java.util.List;


import com.in.mobilerechargedth.adapter.ChannelNumberSearchScreenListViewAdapter;
import com.in.mobilerechargedth.api.ChannelApi;
import com.in.mobilerechargedth.constant.ConstantVariables;
import com.in.mobilerechargedth.model.ChannelModel;
import com.in.mobilerechargedth.model.ThirdScreenAdapterModel;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ChannelNumberSearchScreenActivity extends BaseActivity {

    List<ThirdScreenAdapterModel> rechargeDetails;
    ListView lvChannel;
    TextView txtEmpty;
    int operatorId = 0;
    Button backbtn;
    SearchView searchView;
    ChannelNumberSearchScreenListViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third_screen);

        String strOperatorId = getIntent().getStringExtra(ConstantVariables.OPERATOR_ID);
        operatorId = Integer.parseInt(strOperatorId);
        Log.e("ThirdScreenActivity onCreate", "operatorId: " + operatorId);
        lvChannel = (ListView) findViewById(R.id.lvChannel);
        searchView = (SearchView) findViewById(R.id.searchView);
        searchView.onActionViewExpanded();
        searchView.clearFocus();
        searchView.setOnQueryTextListener(mSearchViewListener);
        backbtn = (Button) findViewById(R.id.backbtn);
        backbtn.setVisibility(View.VISIBLE);
        backbtn.setOnClickListener(this);

        new ChannelTask().execute(new String [] {});
        setupAdmob();
        share();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        searchView.clearFocus();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        super.onClick(v);
        switch (v.getId()) {
        case R.id.backbtn:
            this.onBackPressed();
            break;
        }
    }

    OnQueryTextListener mSearchViewListener = new OnQueryTextListener() {

        @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub
            if (newText.equals("")) {
                adapter.getFilter().filter(newText);
            }
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub
            Log.e("mSearchViewListener", "query:" + query);
            adapter.getFilter().filter(query);
            return false;
        }

    };



    /**
     * Async Task to proceed Operator.
     */
    private class ChannelTask extends AsyncTask<String, Void, List<ChannelModel>> {

        private ProgressDialog mProgress;


        @Override
        protected void onPreExecute() {
            mProgress = new ProgressDialog(ChannelNumberSearchScreenActivity.this);
            mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgress.setMessage("Loading...");
            mProgress.setCanceledOnTouchOutside(false);
            // Check press back key when showing dialog.
            mProgress.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
            mProgress.show();

            super.onPreExecute();
        }

        @Override
        protected List<ChannelModel> doInBackground(String... params) {
            ChannelApi api = new ChannelApi();
            List<ChannelModel> data = null;
            try {
                data = api.getChannel();
            } catch (Exception e) {
            }

            return data;
        }

        @Override
        protected void onPostExecute(List<ChannelModel> result) {
            super.onPostExecute(result);

            if (result != null) {
                adapter = new ChannelNumberSearchScreenListViewAdapter(ChannelNumberSearchScreenActivity.this, result);
                lvChannel.setAdapter(adapter);

                lvChannel.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int position, long id) {
                        ChannelModel channelModel = adapter.getItem(position);
                        Log.e("aaaaaaaaaaaaaaaaaaa", "channelId: " + channelModel.getChannelName());
                        Intent i = new Intent(ChannelNumberSearchScreenActivity.this,
                                ChannelNumberScreenActivity.class);
                        i.putExtra(ConstantVariables.CHANNELS_NAME, channelModel.getChannelName());
                        Log.e("ChannelNumberSearchScreenActivity setOnItemClickListener onItemClick", "CHANNELS_NAME: " + channelModel.getChannelName());
                        startActivity(i);
                        // TODO Auto-generated method stub

                    }
                });
            }
            if (mProgress != null) {
                mProgress.dismiss();
                mProgress = null;
            }

        }

    }
}
